<template>
  <div class="content">
    <div class="main">
      <div class="head" :style="style">
        <div class="title">远能物资质量检测中心</div>
        <div class="backPrev" @click="backPrevClick"></div>
      </div>
      <div class="container">
        <div class="video-box">
          <div class="left-video">
            <div v-for="(i,index) in leftVideoList" :key="index" class="video-module" :style="style3">
               <img :src="i.src" style="width: 100%;height: 100%;" @click="expandVideo($event)"/>
            </div>
          </div>
          <div class="center-video">
              <img :src="centerVideoSrc" style="width: 100%;height: 100%;"/>
          </div>
          <div class="right-video">
            <div v-for="(i,index) in rightVideoList" :key="index" class="video-module" :style="style3">
              <img :src="i.src" style="width: 100%;height: 100%;" @click="expandVideo($event)"/>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<script>

let timeIntervalId = null
let currentIndex = 0

export default {
  name: "Screen",
  props: {},
  components: {

  },
  computed: {
    style() {
      return {
        backgroundImage: `url(${this.bgStyle})`,
        // backgroundSize: "cover", // 背景图片大小自适应
        // backgroundPosition: "center", // 背景图片居中显示
        backgroundRepeat: "no-repeat", // 确保背景图片不重复
        backgroundSize: "100% 100%",
      };
    },
    style3() {
      return {
        backgroundImage: `url(${this.bgStyle3})`,
        backgroundRepeat: "no-repeat", // 确保背景图片不重复
        backgroundSize: "100% 100%",
      };
    },
  },
  data() {
    return {
      bgStyle: require("@/assets/head.png"),
      bgStyle3: require("@/assets/video-left-module-bg.png"),
      leftVideoList:[{src:''},{src:''},{src:''},{src:''}],
      rightVideoList:[{src:''},{src:''},{src:''},{src:''}],
      centerVideoSrc: '',
      currentPage: 0,
      perSideCount: 4,
      totalVideos:  67,
      switchInterval: 60000,
      allVideos: [],

    };
  },
  methods: {
    // 返回上一页
    backPrevClick () {
      sessionStorage.setItem('videoSrc', "")
      this.$router.push({name: 'Screen'})
    },
    startTimer () {
       timeIntervalId = setInterval(this.nextPage, this.switchInterval);
    },
    endTimer () {

    },
    clearTimer () {
      if (timeIntervalId) {
        clearInterval(timeIntervalId);
        timeIntervalId = null;
      }
    },

    initAllVideos(){
      let ports = [5000, 5001, 5002, 5004];
      for (let i = 0; i < this.totalVideos; i++) {
        let port = ports[i % 4]; 
        this.allVideos.push({
          id: i,
          src: `http://192.168.100.168:${port}/v${i+1}` // 使用随机图片模拟视频缩略图
          // src: `http://192.168.5.12:5000/v${i+1}` // 使用随机图片模拟视频缩略图
        });
      }
    },
    updateDisplayVideos () {
       // 计算当前页的起始索引
      const startIndex = this.currentPage * (this.perSideCount * 2);
      
      // 计算当前页的视频(处理循环)
      const currentVideos = [];
      for (let i = 0; i < this.perSideCount * 2; i++) {
        const index = (startIndex + i) % this.totalVideos;
        currentVideos.push(this.allVideos[index]);
      }
      
      // 分配到左右列表
      this.leftVideoList = currentVideos.slice(0, this.perSideCount);
      console.log("leftVideoList", this.leftVideoList)
      this.rightVideoList = currentVideos.slice(this.perSideCount);
      console.log("rightVideoList", this.rightVideoList)
    },
    nextPage (){
       // 计算总页数
      const totalPages = Math.ceil(this.totalVideos / (this.perSideCount * 2));
      // 切换到下一页，循环
      this.currentPage = (this.currentPage + 1) % totalPages;
      this.updateDisplayVideos();
    },
    expandVideo (event){
      console.log('视频被点击', event);
      // 将点击的视频设为中间视频
      this.centerVideoSrc = event.target.src;
      console.log("视频被点击this.centerVideoSrc", this.centerVideoSrc);
    }
  },
  created() {
  },
  mounted() {
    if(sessionStorage.getItem("videoSrc")){
       this.centerVideoSrc = sessionStorage.getItem("videoSrc");
       console.log("this.centerVideoSrc", this.centerVideoSrc);
    }
    // 初始化视频数据
    this.initAllVideos();
    // 显示第一页视频
    this.updateDisplayVideos();
    this.startTimer();

  },
  beforeDestroy() {
    this.clearTimer();
    // sessionStorage.setItem('videoSrc', "")
  },
};
</script>


<style lang="scss" scoped>
.content {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.main {
  width: 100vw;
  height: 50vh;
  background-color: #000549;
  .head {
    width: 100%;
    color: white;
    height: 8vh;
    position: relative;
    .title {
      text-align: center;
      line-height: 6vh;
      font-size: 22px;
      font-weight: bold;
    }
    .backPrev {
      width: 2vw;
      height: 2vw;
      background-image: url("@/assets/back.png");
      background-repeat: no-repeat;
      background-size: 100% 100%;
      position: absolute;
      top: 3vh;
      right: 2vw;
    }
  }

  .container {
    display: flex;
    height: calc(50vh - 8vh);
    width: 100vw;
    .video-box {
      padding-top:4vh;
      padding-bottom: 8vh;
      padding-left: 3vw;
      padding-right: 3vw;
      height: 30vh;
      width: 100vw;
      display: flex;
      gap:4vw;
      justify-content:space-between;
      align-items: center;
      .left-video {
        width: 28vw;
        height: 30vh;
        display: flex;
        flex-wrap: wrap;
        gap:1vw;
        .video-module {
          height: calc(15vh - 0.5vw);
          width: calc(14vw - 0.5vw);
        }
      }
      .center-video {
        width: 30vw;
        height: 34vh;      
        background-image: url("@/assets/video-center-module-bg.png");
        background-repeat: no-repeat;
        background-size: 100% 100%;
      }
      .right-video {
        width: 28vw;
        height: 30vh;
        display: flex;
        flex-wrap: wrap;
        gap:1vw;
        .video-module {
          height: calc(15vh - 0.5vw);
          width: calc(14vw - 0.5vw);
        }
      }
    }
  }

}
</style>
